<?php

class Relay {

    public $id;
    public $team;
    public $athlete_nr1_id;
    public $athlete_nr2_id;
    public $athlete_nr3_id;
    public $athlete_nr4_id;

    function __construct($team = '', $athleteOneID = 0, $athleteTwoID = 0, $athleteThreeID = 0, $athleteFourID = 0) {
        $this->team = $team;
        $this->athlete_nr1_id = $athleteOneID;
        $this->athlete_nr2_id = $athleteTwoID;
        $this->athlete_nr3_id = $athleteThreeID;
        $this->athlete_nr4_id = $athleteFourID;
    }

    /**
     * Load Relay from db by ID.
     * @param int $id
     * @return Relay
     */
    public static function fromID($id) {
        $instance = new self();
        $instance->loadByID($id);
        return $instance;
    }

    /**
     * Load Relay from database row.
     * @param array $row
     * @return Relay
     */
    public static function fromRow(array $row) {
        $instance = new self();
        $instance->fill($row);
        return $instance;
    }

    private function loadByID($id) {
        $result = mysql_query("SELECT `id`, `team`, `athlete_nr1_id`, `athlete_nr2_id`, `athlete_nr3_id`, `athlete_nr4_id` FROM `relays` WHERE `id`='$id'");
        if (mysql_num_rows($result) == 0) {
            return null;
        }
        $row = mysql_fetch_assoc($result);
        $this->fill($row);
    }

    private function fill(array $row) {
        $this->id = $row['id'];
        $this->team = $row['team'];
        $this->athlete_nr1_id = $row['athlete_nr1_id'];
        $this->athlete_nr2_id = $row['athlete_nr2_id'];
        $this->athlete_nr3_id = $row['athlete_nr3_id'];
        $this->athlete_nr4_id = $row['athlete_nr4_id'];
    }

    public function toArray() {
        return array($this->team, $this->athlete_nr1_id, $this->athlete_nr2_id, $this->athlete_nr3_id, $this->athlete_nr4_id);
    }

    public function getID() {
        return $this->id;
    }

    public function getTeam() {
        return $this->team;
    }

    public function setTeam($team) {
        $this->team = $team;
    }

    public function getAthleteOneID() {
        return $this->athlete_nr1_id;
    }

    public function setAthleteOneID($athleteOneID) {
        $this->athlete_nr1_id = $athleteOneID;
    }

    public function getAthleteTwoID() {
        return $this->athlete_nr2_id;
    }

    public function setAthleteTwoID($athleteTwoID) {
        $this->athlete_nr2_id = $athleteTwoID;
    }

    public function getAthleteThreeID() {
        return $this->athlete_nr3_id;
    }

    public function setAthleteThreeID($athleteThreeID) {
        $this->athlete_nr3_id = $athleteThreeID;
    }

    public function getAthleteFourID() {
        return $this->athlete_nr4_id;
    }

    public function setAthleteFourID($athleteFourID) {
        $this->athlete_nr4_id = $athleteFourID;
    }

        
    public function __toString() {
        return "team: $this->team, athleteOneID: $this->athlete_nr1_id, athleteTwoID: $this->athlete_nr2_id, 'athleteThreeID: $this->athlete_nr3_id, athleteFourID: $this->athlete_nr4_id <br>";
    }

    public function toTableRow() {
        return "<tr><td>$this->team</td><td>$this->athlete_nr1_id</td><td>$this->athlete_nr2_id</td><td>$this->athlete_nr3_id</td><td>$this->athlete_nr4_id</td></tr>";
    }

    public function toTableColumns() {
        return "<td>$this->team</td><td>$this->athlete_nr1_id</td><td>$this->athlete_nr2_id</td><td>$this->athlete_nr3_id</td><td>$this->athlete_nr4_id</td>";
    }

}

?>
